package com.xiaoq.shop.repository.impl;

import com.mongodb.WriteConcern;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.shop.entity.ALiPayConfigEntity;
import com.xiaoq.shop.entity.WXPayConfigEntity;
import com.xiaoq.shop.repository.IPayConfigRepository;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


/**
 * Created by chenjh on 2018/1/10.
 */
@Repository
public class PayConfigRepositoryImpl implements IPayConfigRepository {

    @Autowired
    private Datastore dataStore;

    @Override
    public boolean insertALiPayConfig(ALiPayConfigEntity configEntity) {
        if(configEntity != null && StringUtils.isNotBlank(configEntity.getId())){
            configEntity.setDeleteFlag(ConstantUtil.NOT_DELETE_STATUS);
            dataStore.save(configEntity);
            return true;
        }
        return false;
    }

    @Override
    public ALiPayConfigEntity getALiPayConfigByShopId(String shopId) {
        if(StringUtils.isBlank(shopId)){
            return null;
        }
        Query<ALiPayConfigEntity> query = dataStore.createQuery(ALiPayConfigEntity.class)
                .filter("shopId", shopId)
                .filter("deleteFlag", ConstantUtil.NOT_DELETE_STATUS);
        return query.get();
    }

    @Override
    public boolean deleteAliPayConfigByShopId(String shopId) {
        if(StringUtils.isBlank(shopId)){
            return false;
        }
        Query<ALiPayConfigEntity> query = dataStore.createQuery(ALiPayConfigEntity.class)
                .filter("shopId", shopId)
                .filter("deleteFlag", ConstantUtil.NOT_DELETE_STATUS);
        UpdateOperations<ALiPayConfigEntity> ups = dataStore.createUpdateOperations(ALiPayConfigEntity.class);
        ups.set("updatedAt", System.currentTimeMillis());
        ups.set("deleteFlag", ConstantUtil.DELETE_STATUS);
        UpdateResults updateResults = dataStore.update(query, ups,false, WriteConcern.ACKNOWLEDGED);
        return updateResults.getUpdatedExisting();
    }


    @Override
    public boolean insertWXPayConfig(WXPayConfigEntity configEntity) {
        if(configEntity != null && StringUtils.isNotBlank(configEntity.getId())){
            configEntity.setDeleteFlag(ConstantUtil.NOT_DELETE_STATUS);
            dataStore.save(configEntity);
            return true;
        }
        return false;
    }

    @Override
    public WXPayConfigEntity getWXPayConfigByShopId(String shopId, String wxPayType) {
        if(StringUtils.isBlank(shopId) || StringUtils.isBlank(wxPayType)){
            return null;
        }
        Query<WXPayConfigEntity> query = dataStore.createQuery(WXPayConfigEntity.class)
                .filter("shopId", shopId)
                .filter("wxPayType", wxPayType)
                .filter("deleteFlag", ConstantUtil.NOT_DELETE_STATUS);
        return query.get();
    }

    @Override
    public boolean deleteWXPayConfigByShopId(String shopId, String wxPayType) {
        if(StringUtils.isBlank(shopId) || StringUtils.isBlank(wxPayType)){
            return false;
        }
        Query<WXPayConfigEntity> query = dataStore.createQuery(WXPayConfigEntity.class)
                .filter("shopId", shopId)
                .filter("wxPayType", wxPayType)
                .filter("deleteFlag", ConstantUtil.NOT_DELETE_STATUS);
        UpdateOperations<WXPayConfigEntity> ups = dataStore.createUpdateOperations(WXPayConfigEntity.class);
        ups.set("updatedAt", System.currentTimeMillis());
        ups.set("deleteFlag", ConstantUtil.DELETE_STATUS);
        UpdateResults updateResults = dataStore.update(query, ups,false, WriteConcern.ACKNOWLEDGED);
        return updateResults.getUpdatedExisting();
    }
}
